package bean;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import javax.validation.Valid;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import validation.DoubleNameCheck;
import validation.KreditCheck;

@KreditCheck
public class Kunde {
    
    @NotNull(message=":: nicht null") @Size(min=1, message=":: Name nicht leer")
    private String name;
    
    @DecimalMin(value="0",  message=":: muss positiv sein")
    private BigInteger maxKredit = new BigInteger("0");
    
    @Valid
    @Size(max=4, message=":: nicht mehr als vier") 
    @DoubleNameCheck
    private List<Kreditgeber> kredite = new ArrayList<>();
    
    public Kunde() {
    }
    
    public void addKredit(Kreditgeber kg) {
        
        this.kredite.add(kg);
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the maxKredit
     */
    public BigInteger getMaxKredit() {
        return maxKredit;
    }

    /**
     * @param maxKredit the maxKredit to set
     */
    public void setMaxKredit(BigInteger maxKredit) {
        this.maxKredit = maxKredit;
    }

    /**
     * @return the kredite
     */
    public List<Kreditgeber> getKredite() {
        return kredite;
    }

    /**
     * @param kredite the kredite to set
     */
    public void setKredite(List<Kreditgeber> kredite) {
        this.kredite = kredite;
    }
    
    @Override
    public String toString() {
        StringBuilder text = new StringBuilder();
        BigInteger summe = new BigInteger("0");
        
        text.append(this.name).append(" max:").append(this.maxKredit).append(" Kredite:\n");
        for(int i=0; i<this.kredite.size(); i++) {
            Kreditgeber kg = this.kredite.get(i);
            text.append("(").append(i+1).append(") ").append(kg.getName()).
                    append(" Summe: ").append(kg.getSumme()).append("\n");
            summe = summe.add(kg.getSumme());
        }
        text.append("Kreditsumme: ").append(summe).append("\n");
        
        return text.toString();
    }
}
